#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;

class Solution{
    public:
    //my ans
    string ans1(string s, int numRows){
        int length = s.size();
        int step = 2 * numRows - 2;
        string out = "";
        if(numRows == 1){
            out = s;
        }
        else{
            int numCols = (length / step + 1) * (numRows - 1);
            char ch[numRows][numCols];
            for(int i = 0; i != numRows; i++){
                for(int j = 0; j != numCols; j++){
                    ch[i][j] = '\0';
                }
            }

            int row = -1, col = -1;
            for(int i = 0; i != length; i++){
                int temp = i % step;
                if(temp == 0){
                    row = -1;
                    col++;
                }
                if(temp >= 0 && temp <= numRows-1){
                    row++;
                    ch[row][col] = s[i];
                }else{
                    row--;
                    col++;
                    ch[row][col] = s[i];
                }
            }
            for(int i = 0; i != numRows; i++){
                for(int j = 0; j != numCols; j++){
                    if(ch[i][j] != '\0'){
                        out = out + ch[i][j];
                    }
                }
            }
        }
        return out;
    }

};

void test1(){
    Solution s;
    string str = "PAYPALISHIRING";
    int numRows = 3;
    string ret = s.ans1(str, numRows);
    cout << ret << endl;
}

void test2(){
    Solution s;
    string str = "PAYPALISHIRING";
    int numRows = 4;
    string ret = s.ans1(str, numRows);
    cout << ret << endl;
}

int main(void){
    test1();
    test2();

    return 0;
}